package com.wmyg.antifake.controller;

import com.wmyg.antifake.bean.Factory;
import com.wmyg.antifake.bean.User;
import com.wmyg.antifake.service.FactoryService;
import com.wmyg.antifake.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FactoryService factoryService;

    @RequestMapping("index")
    public ModelAndView toLogin(ModelAndView mv) {
        mv.setViewName("loginb");
        return mv;
    }

    @RequestMapping(value = "logout")
    public ModelAndView logout(ModelAndView mv,HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("loginUser");
        mv.setViewName("loginb");
        return mv;
    }

    @RequestMapping(value = "user/index")
    public ModelAndView loginIndex(ModelAndView mv) {
        mv.setViewName("user/admin");
        return mv;
    }

    @RequestMapping(value="/getUname",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getUname(HttpServletRequest request){
        Map result = new HashMap<String,Object>();
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("loginUser");
        result.put("USERNAME",user.getUserName());
        result.put("USERID",user.getUserId());
        return result;
    }

    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> login(HttpServletRequest request, User user){
        Map result = new HashMap<String,Object>();
        result.put("result","success");
        String userName = user.getUserName().trim();
        String password = user.getUserPwd().trim();
        User tUser = userService.findUserByNameAndPwd(userName,password);

        if(tUser != null){
            HttpSession session = request.getSession();
            session.setAttribute("loginUser",tUser);
        }
        else{
            result.put("result","usererror");
            return result;
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.GET,value = "user/toReg")
    public ModelAndView toReg(ModelAndView mv){
        List<Factory> factories = factoryService.findAll();
        mv.addObject("factories",factories);
        mv.setViewName("user/reg");
        return mv;
    }

    @RequestMapping(method = RequestMethod.POST,value = "user/reg")
    public ModelAndView reg(ModelAndView mv,HttpServletRequest request,User user){
        HttpSession session = request.getSession();
        User sessUser = (User)session.getAttribute("loginUser");
        String userName = user.getUserName();
        user.setUserType(2);
        List<User> userList = userService.selectByUserName(userName);
        if(userList != null && userList.size() > 0){
            List<Factory> factories = factoryService.findAll();
            mv.addObject("factories",factories);
            mv.addObject("error",userName+",用户名已经存在了，请修改之！");
            mv.setViewName("user/reg");
            return mv;
        }
        userService.insert(user);
        mv.setViewName("success");
        return mv;
    }

    @RequestMapping(method = RequestMethod.GET,value = "user/toUpdate")
    public ModelAndView toUpdate(HttpServletRequest request,ModelAndView mv){
        HttpSession session = request.getSession();
        User sessUser = (User)session.getAttribute("loginUser");
        mv.addObject("user",sessUser);
        mv.setViewName("user/modify");
        return mv;
    }

    @RequestMapping(method = RequestMethod.POST,value = "user/update")
    public ModelAndView update(ModelAndView mv,HttpServletRequest request,User user,String oldUserPwd){
        HttpSession session = request.getSession();
        User sessUser = (User)session.getAttribute("loginUser");
        if(!sessUser.getUserPwd().equals(oldUserPwd)){
            mv.addObject("error","旧密码输入错误，请重新输入！");
            mv.setViewName("user/modify");
            return mv;
        }
        user.setUserId(sessUser.getUserId());
        userService.update(user);
        mv.setViewName("success");
        return mv;
    }

    @RequestMapping(method = RequestMethod.GET,value = "/delete/{id}")
    public void delete(@PathVariable("id")int id){
        userService.delete(id);
    }


    @RequestMapping(method = RequestMethod.GET,value = "/{id}/select")
    public User select(@PathVariable("id")int id){
        return userService.selectById(id);
    }

    @RequestMapping(method = RequestMethod.GET,value = "/selectAll/{pageNum}/{pageSize}")
    public List<User> selectAll(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        return userService.selectAll(pageNum,pageSize);
    }

    @RequestMapping(value = "show.do")
    public ModelAndView show(ModelAndView mv) {
        mv.setViewName("index");
        return mv;
    }

}
